package com.lysj.bhp.exchangerate.service;

import com.lysj.bhp.exchangerate.base.ExchangeRateContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

/**
 * 汇率相关服务类
 */
@Service
public class ExchangeRateService extends ExchangeRateContent {

    private static Logger logger = LoggerFactory.getLogger(ExchangeRateService.class);

    /**
     * 获取算力币与美元的汇率
     *
     * @return
     */
    public BigDecimal getBhpcToDollar() {
        long time = exchangeRate.getBHPC_TO_DOLLAR_EXPIRES();
        BigDecimal rate = exchangeRate.getBHPC_TO_DOLLAR();
        if (System.currentTimeMillis() < time) { //汇率还在有效期内
            return rate;
        }
        BigDecimal bhpcToEth = exchangeRate.getBHPC_TO_ETH(); //获取算力币对以太坊汇率
        BigDecimal ethereumToDollar = exchangeRate.getETHEREUM_TO_DOLLAR(); //获取以太币对应美元汇率
        BigDecimal ethToDollar = bhpcToEth.multiply(ethereumToDollar); //获取算力币对美元汇率
        logger.info(ethToDollar + "算力币对美元汇率----");
        rate = ethToDollar;
        return rate;
    }

    /**
     * 获取BHPC与人民币的汇率
     *
     * @return
     */
    public BigDecimal bhpcToCnyRate() {
        long time = exchangeRate.getBHPC_TO_CNY_EXPIRES();
        BigDecimal rate = exchangeRate.getBHPC_TO_CNY();
        if (System.currentTimeMillis() < time) { //汇率还在有效期内
            return rate;
        }
        rate = getBhpcToDollar().multiply(exchangeRate.getDOLLAR_TO_CNY());
        logger.info(rate + "BHPC与人民币的汇率----");
        return rate;
    }


    /**
     * @author drj
     * @date 2018/05/30 20:06
     * @Description:获取人民币与美元的汇率
     */
    public BigDecimal cnyToDollarRate() {
        long time = exchangeRate.getCNY_TO_DOLLAR_EXPIRES();
        BigDecimal rate = exchangeRate.getCNY_TO_DOLLAR();
        if (System.currentTimeMillis() < time) { //汇率还在有效期内
            return rate;
        }
        rate = new BigDecimal("1").divide(exchangeRate.getDOLLAR_TO_CNY(), 18, BigDecimal.ROUND_HALF_UP);
        logger.info(rate + "人民币与美元的汇率----");
        return rate;
    }


    /**
     * @author wtl
     * @date 2018-06-07 19:33
     * @Description:获取比特币对人民币的汇率
     */
    public BigDecimal bitcoinToCnyRate() {
        long time = exchangeRate.getBITCOIN_TO_CNY_EXPIRES();
        BigDecimal rate = exchangeRate.getBITCOIN_TO_CNY();
        if (System.currentTimeMillis() < time) { //汇率还在有效期内
            return rate;
        }
        rate = exchangeRate.getBITCOIN_TO_DOLLAR().multiply(exchangeRate.getDOLLAR_TO_CNY());
        logger.info(rate + "比特币对人民币的汇率----");
        return rate;
    }


    /**
     * @author wtl
     * @date 2018-06-07 19:33
     * @Description:获取以太坊对人民币的汇率
     */
    public BigDecimal ethToCnyRate() {
        long time = exchangeRate.getETH_TO_CNY_EXPIRES();
        BigDecimal rate = exchangeRate.getETH_TO_CNY();
        if (System.currentTimeMillis() < time) { //汇率还在有效期内
            return rate;
        }
        rate = exchangeRate.getETHEREUM_TO_DOLLAR().multiply(exchangeRate.getDOLLAR_TO_CNY());
        logger.info(rate + "以太坊对人民币的汇率----");
        return rate;
    }

    /**
     * @author wtl
     * @date 2018-07-25 9:56
     * @Description 获取汇率
     */
    public void getRate(int status){
        if(status == 1){
            if(ETHEREUM_TO_DOLLAR_ISRATE == 1){
                exchangeRate.setETHEREUM_TO_DOLLAR(bcexService.getEthereumToDollar());
            }
            if(BITCOIN_TO_DOLLAR_ISRATE == 1){
                exchangeRate.setBITCOIN_TO_DOLLAR(bcexService.getBitcoinToDollar());
            }
            if(BHPC_TO_ETH_ISRATE == 1){
                exchangeRate.setBHPC_TO_ETH(bcexService.getBhpcToEth());
            }
            if(DOLLAR_TO_CNY_ISRATE == 1){
                exchangeRate.setDOLLAR_TO_CNY(bcexService.dollarToCnyRate());
            }
        }else if(status == 2){
            if(ETHEREUM_TO_DOLLAR_ISRATE == 1){
                exchangeRate.setETHEREUM_TO_DOLLAR(digifinexService.getEthereumToDollar());
            }
            if(BITCOIN_TO_DOLLAR_ISRATE == 1){
                exchangeRate.setBITCOIN_TO_DOLLAR(digifinexService.getBitcoinToDollar());
            }
            if(BHPC_TO_ETH_ISRATE == 1){
                exchangeRate.setBHPC_TO_ETH(digifinexService.getBhpcToEth());
            }
            if(DOLLAR_TO_CNY_ISRATE == 1){
                exchangeRate.setDOLLAR_TO_CNY(digifinexService.dollarToCnyRate());
            }
        }else if(status == 3){
            if(ETHEREUM_TO_DOLLAR_ISRATE == 1){
                exchangeRate.setETHEREUM_TO_DOLLAR(bcexproxyService.getEthereumToDollar());
            }
            if(BITCOIN_TO_DOLLAR_ISRATE == 1){
                exchangeRate.setBITCOIN_TO_DOLLAR(bcexproxyService.getBitcoinToDollar());
            }
            if(BHPC_TO_ETH_ISRATE == 1){
                exchangeRate.setBHPC_TO_ETH(bcexproxyService.getBhpcToEth());
            }
            if(DOLLAR_TO_CNY_ISRATE == 1){
                exchangeRate.setDOLLAR_TO_CNY(bcexproxyService.dollarToCnyRate());
            }
        }
        if(BHPC_TO_CNY_ISRATE == 1){
            exchangeRate.setBHPC_TO_CNY(bhpcToCnyRate());
        }
        if(CNY_TO_DOLLAR_ISRATE == 1){
            exchangeRate.setCNY_TO_DOLLAR(cnyToDollarRate());
        }
        if(BITCOIN_TO_CNY_ISRATE == 1){
            exchangeRate.setBITCOIN_TO_CNY(bitcoinToCnyRate());
        }
        if(ETH_TO_CNY_ISRATE == 1){
            exchangeRate.setETH_TO_CNY(ethToCnyRate());
        }
        if(BHPC_TO_DOLLAR_ISRATE == 1){
            exchangeRate.setBHPC_TO_DOLLAR(getBhpcToDollar());
        }
    }

}
